package com.esdut.ciremember;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Company  CiJI
 * @Classname
 * @Description TODO 启动倒计时活动
 * @Author wenXin
 * Date 2021.6.28 10:01
 * Version 1.0
 */

public class SplashActivity extends AppCompatActivity {
    private TextView tvTimer;
    private int recTimes = 4;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                recTimes --;
                tvTimer.setText(""+recTimes);
                if(recTimes==0){
                    //倒计时结束，活动跳转
                    next();
                }
            }
        }
    };

    /**
     * @methodName TODO 倒计时结束方法
     * @author wenXin
     * @date 2021.6.29 11:08
     * @return void
     */
    private void next() {
        //活动跳转
        startActivity(new Intent(com.esdut.ciremember.SplashActivity.this, MainActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //去除状态栏2
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //关联布局
        setContentView(R.layout.activity_splash);
        
        init();

        new Thread(new MyTimer()).start();
    }

    /**
     * @methodName TODO 查询控件：初始化
     *  
     * @author wenXin
     * @date 2021.6.29 15:00
     * @return void
     */
    private void init() {
        tvTimer = findViewById(R.id.tvTimer);
    }

    //实现倒计时的多线程类： extends Thread & implements Runnable
    class MyTimer implements Runnable {
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(recTimes==0){
                    break;
                }
                //发消息
                handler.sendEmptyMessage(0);
            }
        }
    }
}